package org.jeecg.modules.activiti.listener;

import com.alibaba.fastjson.JSON;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.activiti.entity.ActBusiness;
import org.jeecg.modules.activiti.service.Impl.ActBusinessServiceImpl;
import org.jeecg.modules.zcgl.entity.ReceivablesInfo;
import org.jeecg.modules.zcgl.service.IReceivablesInfoService;
import org.jeecg.modules.zcgl.service.impl.ReceivablesInfoServiceImpl;
import org.springframework.stereotype.Component;

/**
 * 应收款变更表单审批后监听
 */
@Component
public class ReceivablesEditListener implements ExecutionListener {

    ActBusinessServiceImpl actBusinessService;
    private IReceivablesInfoService receivablesInfoService;

    public void notify(DelegateExecution execution) throws Exception {
        if (receivablesInfoService == null){
            receivablesInfoService = (ReceivablesInfoServiceImpl)SpringContextUtils.getBean("receivablesInfoServiceImpl");
        }
        if (actBusinessService == null){
            actBusinessService = (ActBusinessServiceImpl) SpringContextUtils.getBean("actBusinessServiceImpl");
        }

        String eventName = execution.getEventName();
        //start
        if (EVENTNAME_START.equals(eventName)) {
        } else if (EVENTNAME_END.equals(eventName)) {
            ActBusiness actBusiness = actBusinessService.getById(execution.getProcessBusinessKey());
            if("2".equals(actBusiness.getStatus())){

                if (actBusiness != null && "zcgl_receivables_info".equalsIgnoreCase(actBusiness.getTableName())){
                    ReceivablesInfo info = JSON.parseObject(actBusiness.getData(), ReceivablesInfo.class);
//                receivablesInfoService.checkModel(info);
                    receivablesInfoService.saveOrUpdateById(info);
                }
            }


        }
//        System.out.println("----" + execution.getProcessBusinessKey() + "" + execution.getProcessDefinitionId());
    }
}
